﻿#define DEBUG
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Activities;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Collections;
using System.IO;
using WFDatabase.Helpers;
using System.ComponentModel;
using System.Activities.Presentation.Metadata;
using WFDataBaseProject.Helpers;
using System.Activities.Presentation.PropertyEditing;
using WFDataBaseProject;

namespace WFDatabaseProject
{
    [Designer(typeof(UCStatisticsActivityDesigner))]
    public class StatisticActivity: CodeActivity
    {
        public StatisticActivity()
        {
            AttributeTableBuilder builder = new AttributeTableBuilder();
            builder.AddCustomAttributes(typeof(StatisticActivity), "ConnectionString",
                    new EditorAttribute(typeof(ConnectionStringEditor),
                    typeof(DialogPropertyValueEditor)));
            MetadataStore.AddAttributeTable(builder.CreateTable());
        }
        #region arguments
        [RequiredArgument]
        [Category("Inputs")]
        [Description("The connection to connect to the server")]
        public InArgument<string> ConnectionString { get; set; }
        [RequiredArgument]
        [Category("Inputs")]
        [Description("The filter to retrieve special kindofstatistics")]
        public InArgument<StatisticsAttributes> Filter { get; set; }
        [Browsable(false)]
        public OutArgument<ArrayList> Statisitcs { get; set; }
        #endregion
        #region fields
        string connectionstring;
        StatisticsAttributes filter;
        #endregion
        
        protected override void Execute(CodeActivityContext context)
        {
            SqlConnection connection;
            connectionstring = ConnectionString.Get<string>(context);
            filter = Filter.Get<StatisticsAttributes>(context);
            connection = new SqlConnection(connectionstring);
            try
            {
                
                connection.Open();
                if (connection.StatisticsEnabled == false) connection.StatisticsEnabled = true;
                if (connection.StatisticsEnabled == true) connection.ResetStatistics();
                ArrayList listOfStatisticObjects = new ArrayList();
                switch (filter)
                {
                    case StatisticsAttributes.ExecutionTime:
                        listOfStatisticObjects.Add(connection.RetrieveStatistics("ExecutionTime"));
                        break;
                    case StatisticsAttributes.Transactions:
                        listOfStatisticObjects.Add(connection.RetrieveStatistics("Transactions"));
                        break;
                    case StatisticsAttributes.UnpreparedExecs:
                        listOfStatisticObjects.Add(connection.RetrieveStatistics("UnpreparedExecs"));
                        break;
                    case StatisticsAttributes.BuffersSent:
                        listOfStatisticObjects.Add(connection.RetrieveStatistics("BuffersSent"));
                        break;
                    case StatisticsAttributes.PreparedExecs:
                        listOfStatisticObjects.Add(connection.RetrieveStatistics("PreparedExecs"));
                        break;
                    case StatisticsAttributes.SelectCount:
                        listOfStatisticObjects.Add(connection.RetrieveStatistics("SelectCount"));
                        break;
                    case StatisticsAttributes.ConnectionTime:
                        listOfStatisticObjects.Add(connection.RetrieveStatistics("ConnectionTime"));
                        break;
                    case StatisticsAttributes.IduRows:
                        listOfStatisticObjects.Add(connection.RetrieveStatistics("IduRows"));
                        break;
                    case StatisticsAttributes.Prepares:
                        listOfStatisticObjects.Add(connection.RetrieveStatistics("Prepares"));
                        break;
                    case StatisticsAttributes.SelectRows:
                        listOfStatisticObjects.Add(connection.RetrieveStatistics("SelectRows"));
                        break;
                    case StatisticsAttributes.ServerRoundtrips:
                        listOfStatisticObjects.Add(connection.RetrieveStatistics("ServerRoundtrips"));
                        break;
                    case StatisticsAttributes.NetworkServerTime:
                        listOfStatisticObjects.Add(connection.RetrieveStatistics("NetworkServerTime"));
                        break;
                    case StatisticsAttributes.BytesReceived:
                        listOfStatisticObjects.Add(connection.RetrieveStatistics("BytesReceived"));
                        break;
                    case StatisticsAttributes.IduCount:
                        listOfStatisticObjects.Add(connection.RetrieveStatistics("IduCount"));
                        break;
                    case StatisticsAttributes.CursorOpens:
                        listOfStatisticObjects.Add(connection.RetrieveStatistics("CursorOpens"));
                        break;
                    case StatisticsAttributes.SumResultSets:
                        listOfStatisticObjects.Add(connection.RetrieveStatistics("SumResultSets"));
                        break;
                    case StatisticsAttributes.BytesSent:
                        listOfStatisticObjects.Add(connection.RetrieveStatistics("BytesSent"));
                        break;
                    case StatisticsAttributes.BuffersReceived:
                        listOfStatisticObjects.Add(connection.RetrieveStatistics("BuffersReceived"));
                        break;
                    case StatisticsAttributes.All:
                        listOfStatisticObjects.Add(connection.RetrieveStatistics("All"));
                        break;
                    default:
                        listOfStatisticObjects.Add(connection.RetrieveStatistics("All"));
                        break;
                }

                Statisitcs.Set(context, listOfStatisticObjects);
#if DEBUG
                foreach (var item in listOfStatisticObjects)
                {
                    Console.WriteLine("{0}",item.ToString());
                }        
#endif

            
            }
            catch (SqlException caught)
            {
#if DEBUG
                Debug.Print(caught.Message);
#endif
            }
            finally
            {
                connection.Close();
            }
        }
    }
}
